layui.define(['layer', 'element', 'form'], function (exports) {
    var layer = layui.layer;
    var element = layui.element;
    var form = layui.form;
    var popupRightIndex, popupCenterIndex, popupCenterParam, popupRightParam;

    var app = {
        // 设置侧栏折叠
        flexible: function (expand) {
            var isExapnd = $('.layui-layout-admin').hasClass('admin-nav-mini');
            if (isExapnd == !expand) {
                return;
            }
            if (expand) {
                $('.layui-layout-admin').removeClass('admin-nav-mini');
            } else {
                $('.layui-layout-admin').addClass('admin-nav-mini');
            }
            app.onResize();
        },
        // 右侧弹出
        popupRight: function (param) {
            popupRightParam = param;
            let $width = (typeof param.width) == "undefined" ? '600px' : param.width;
            popupRightIndex = layer.open({
                type: 1,
                id: 'adminPopupR',
                anim: 2,
                isOutAnim: false,
                title: false,
                /*closeBtn: false,*/
                offset: 'r',
                shade: .2,
                shadeClose: true,
                resize: false,
                area: $width,
                skin: 'layui-layer-adminRight set-close-layer',
                success: function () {
                    $('#adminPopupR').load(param.path + '?t=' + Math.random(), function () {
                        $('#adminPopupR .close').click(function () {
                            layer.close(popupCenterIndex);
                        });
                        param.init ? param.init() : '';
                    });
                },
                btn: param.btn ? param.btn : ['操 作', '关 闭'],
                yes: function (index, layero) {
                    param.opt ? param.opt() : '';
                }
                , btn2: function (index, layero) {

                },
                end: function () {
                    layer.closeAll('tips');
                }
            });
        },
        // 关闭右侧弹出
        closePopupRight: function () {
            layer.close(popupRightIndex);
        },
        // 中间弹出
        popupCenter: function (param) {
            popupCenterParam = param;
            let $height = (typeof param.height) == "undefined" ? '80%' : (param.height == 0 ? '80%' : param.height);
            let $width = (typeof param.width) == "undefined" ? '600px' : param.width;
            let $area = [$width, $height]
            popupCenterIndex = layer.open({
                // type: 1,
                id: 'adminPopupC',
                title: param.title ? param.title : false,
                shade: .2,
                offset: '50px',
                area: param.area ? param.area : $area,
                resize: false,
                scrollbar: false,

                success: function () {
                    $('#adminPopupC').load(param.path + '?t=' + Math.random(), function () {
                        $('#adminPopupC .close').click(function () {
                            layer.close(popupCenterIndex);
                        });
                        param.init ? param.init() : '';
                    });
                },
                btn: param.btn ? param.btn : ['操 作', '关 闭'],
                yes: function (index, layero) {
                    param.opt ? param.opt() : '';
                }
                , btn2: function (index, layero) {

                },
                end: function () {
                    layer.closeAll('tips');
                    param.end ? param.end() : '';
                }
            });
        },
        // 关闭中间弹出并且触发finish回调
        finishPopupCenter: function () {
            layer.close(popupCenterIndex);
            popupCenterParam.finish ? popupCenterParam.finish() : '';
        },
        finishForm: function () {
            layer.close(popupCenterIndex);
            (popupCenterParam && popupCenterParam.finish) ? popupCenterParam.finish() : (popupRightParam && popupRightParam.finish) ? popupRightParam.finish : '';
        },
        // 关闭中间弹出
        closePopupCenter: function () {
            layer.close(popupCenterIndex);
        },
        // 窗口大小改变监听
        onResize: function () {
            if (false) {
                if ($('.layui-table-view').length > 0) {
                    setTimeout(function () {
                        app.events.refresh();
                    }, 800);
                }
            }
        },
        // 显示加载动画
        showLoading: function (element) {
            $(element).append('<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop admin-loading"></i>');
        },
        // 移除加载动画
        removeLoading: function (element) {
            $(element + '>.admin-loading').remove();
        },
        // 公共表单窗体
        form: function (_param) {
            var path = '/components/tpl/common_form.html';
            if (_param.popType == 2) {
                path = '/components/tpl/common_form_multiple.html'
            } else if (_param.popType == 3) {
                path = '/components/tpl/common_form_tab.html'
            }
            if (_param.popType == 3) {
                app.popupRight({
                    title: _param.title,
                    width: (typeof _param.width) == "undefined" ? '600px' : _param.width,
                    height: (typeof _param.height) == "undefined" ? '80%' : _param.height,
                    path: path,
                    finish: function () {
                        _param.finish ? _param.finish() : '';
                    },
                    init: function () {
                        _param.init ? _param.init() : '';
                    },
                    opt: function () {
                        _param.opt ? _param.opt() : '';
                    }
                });
            }
            else {
                app.popupCenter({
                    title: _param.title,
                    height: (typeof _param.height) == "undefined" ? '80%' : _param.height,
                    width: (typeof _param.width) == "undefined" ? '600px' : _param.width,
                    path: path,
                    finish: function () {
                        _param.finish ? _param.finish() : '';
                    },
                    init: function () {
                        _param.init ? _param.init() : '';
                    },
                    opt: function () {
                        _param.opt ? _param.opt() : '';
                    }
                });
            }
        },
        // 自定义页面窗体
        page: function (_param) {
            app.popupCenter({
                title: _param.title,
                height: (typeof _param.height) == "undefined" ? '80%' : _param.height,
                path: _param.url,
                finish: function () {
                    _param.finish ? _param.finish() : '';
                },
                init: function () {
                    _param.init ? _param.init() : '';
                },
                opt: function () {
                    _param.opt ? _param.opt() : '';
                }
            });
        },
        // 公共详情窗体
        info: function (_param) {
            app.popupCenter({
                title: '',
                height: (typeof _param.height) == "undefined" ? '80%' : _param.height,
                width: (typeof _param.width) == "undefined" ? '600' : _param.width,
                path: '/components/tpl/common_info.html',
                btn: [],
                init: function () {
                    _param.init ? _param.init() : '';
                }
            });
        }
    };

    // 事件
    app.events = {
        flexible: function (e) {  // 折叠侧导航
            var expand = $('.layui-layout-admin').hasClass('admin-nav-mini');
            app.flexible(expand);
        },
        refresh: function () {  // 刷新主体部分
            Q.refresh();
        },
        back: function () {  //后退
            history.back();
        },
        fullScreen: function (e) {  // 全屏
            var ac = 'layui-icon-screen-full', ic = 'layui-icon-screen-restore';
            var ti = $(this).find('i');

            var isFullscreen = document.fullscreenElement || document.msFullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || false;
            if (isFullscreen) {
                var efs = document.exitFullscreen || document.webkitExitFullscreen || document.mozCancelFullScreen || document.msExitFullscreen;
                if (efs) {
                    efs.call(document);
                } else if (window.ActiveXObject) {
                    var ws = new ActiveXObject('WScript.Shell');
                    ws && ws.SendKeys('{F11}');
                }
                ti.addClass(ac).removeClass(ic);
            } else {
                var el = document.documentElement;
                var rfs = el.requestFullscreen || el.webkitRequestFullscreen || el.mozRequestFullScreen || el.msRequestFullscreen;
                if (rfs) {
                    rfs.call(el);
                } else if (window.ActiveXObject) {
                    var ws = new ActiveXObject('WScript.Shell');
                    ws && ws.SendKeys('{F11}');
                }
                ti.addClass(ic).removeClass(ac);
            }
        }
    };

    // 所有ew-event
    $('body').on('click', '*[ew-event]', function () {
        var event = $(this).attr('ew-event');
        var te = app.events[event];
        te && te.call(this, $(this));
    });

    // 移动设备遮罩层点击事件
    $('.site-mobile-shade').click(function () {
        app.flexible(true);
    });

    // 侧导航折叠状态下鼠标经过显示提示
    $('body').on('mouseenter', '.layui-layout-app.admin-nav-mini .layui-side .layui-nav .layui-nav-item>a', function () {
        var tipText = $(this).find('cite').text();
        if (document.body.clientWidth > 750) {
            layer.tips(tipText, this);
        }
    }).on('mouseleave', '.layui-layout-app.admin-nav-mini .layui-side .layui-nav .layui-nav-item>a', function () {
        layer.closeAll('tips');
    });

    // 侧导航折叠状态下点击展开
    $('body').on('click', '.layui-layout-app.admin-nav-mini .layui-side .layui-nav .layui-nav-item>a', function () {
        if (document.body.clientWidth > 750) {
            layer.closeAll('tips');
            app.flexible(true);
        }
    });

    // 所有lay-tips处理
    $('body').on('mouseenter', '*[lay-tips]', function () {
        var tipText = $(this).attr('lay-tips');
        var dt = $(this).attr('lay-direction');
        layer.tips(tipText, this, {tips: dt || 1, time: -1});
    }).on('mouseleave', '*[lay-tips]', function () {
        layer.closeAll('tips');
    });

    exports('app', app);
});
